package org.ithang.application.customer.service;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ithang.application.account.bean.Account;
import org.ithang.application.account.service.AccountService;
import org.ithang.application.bcAddr.bean.BcAddr;
import org.ithang.application.bcAddr.bean.EthAddr;
import org.ithang.application.bcAddr.service.BcAddrService;
import org.ithang.application.customer.bean.Customer;
import org.ithang.application.customer.bean.Team;
import org.ithang.application.customer.mapper.CustomerMapper;
import org.ithang.application.grade.service.GradeService;
import org.ithang.application.teamBonus.service.TeamBonusService;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.database.ModelDao;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.lang.StrUtils;
import org.ithang.tools.model.Pager;
import org.ithang.tools.sch.AppSchedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户表信息 服务
 * @author zyt
 *
 */
@Service
public class CustomerService extends ModelDao<Customer>{

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private AccountService accountService;
    
    @Autowired
    private BcAddrService bcAddrService;
    
    @Autowired
    private KeyvalueService keyvalueService;
    
    @Autowired
	private GradeService gradeService;
    
    @Autowired
    private TeamBonusService teamBonusService;
    
    
    
    private Logger logger = Logger.getLogger(CustomerService.class);
    
    @Transactional
    public void reg(Customer user,Customer fromUser)throws Exception{
		add(user);
		String id=String.valueOf(user.getId());
		int len=6-id.length();
		if(len>0){//最多生成99万个不重复邀请码
		    user.setMycode(StrUtils.randomNumberCode(len)+id);	
		}else{//如果用户突破100万，生成8位邀请码
			len=8-id.length();
			user.setMycode(StrUtils.randomNumberCode(len)+id);
		}
		update(user);
		Account account=new Account();
		account.setCustomer_id(user.getId());
		account.setAmount(0D);
		account.setFrozen_amount(0D);
		account.setInvest_amount(0D);
		account.setCurrency_id(1);
		account.setCreate_time(DateUtils.getSystime());
		if(null!=fromUser){//设置邀请人
			Account fromAccount=accountService.getByCustomer(fromUser.getId());
			
			if(null!=fromAccount){
                account.setFrom_user(fromAccount.getId());
			}	
			
			/*fromUser.setMyusers((null==fromUser.getMyusers()?1:fromUser.getMyusers()+1));//发展人数+1
			update(fromUser);
			
			Customer c=get(fromUser.getId());
			c=get(fromUser.getFrom_user());
			while(null!=c&&null!=c.getFrom_user()&&c.getFrom_user()>0){//如果fromUser有上线，则上线myusers+1
				c=get(c.getFrom_user());
				if(null!=c){
					c.setMyusers((null==c.getMyusers()?1:c.getMyusers()+1));//fromUser发展人数+1	
				}
				update(c);
				if(c.getFrom_user()>0){
					c=get(c.getFrom_user());
				}else{
					c=null;
				}
			}*/
			
		}else{
			account.setFrom_user(0);
		}
		accountService.add(account);
		
		try{
		    //创建钱包地址
		    BcAddr bcAddr=new BcAddr();
		    bcAddr.setCustomer_id(user.getId());
		    bcAddr.setCurrency_id(1);
		    bcAddrService.add(bcAddr);
		    AppSchedule.addrs.add(new EthAddr(bcAddr.getAddr()));
		}catch(Exception e){
			e.printStackTrace();
		}
		
    }
    
    
   
    
    
    public void add(Customer customer){
        customerMapper.add(customer);
    }
    
    public Customer get(Integer id){
        return customerMapper.get(id);
    }
    
    public Customer getByFromUser(Integer fromUserID){
    	return customerMapper.getByFromUser(fromUserID);
    }
    
    public Customer getByMobile(String mobile){
    	return customerMapper.getByMobile(mobile);
    }
    
    public Customer getByCode(String code){
    	return customerMapper.getByCode(code);
    }
    
    public Customer getByName(String uname){
    	return customerMapper.getByName(uname);
    }
    
    public int delete(Integer id){
        return customerMapper.delete(id);
    }
    
    public int batchDelete(String[] ids){
    	return customerMapper.batchDelete(ids);
    }
    
    public List<Customer> list(Integer... ids){
    	return customerMapper.list(ids);
    }
    
    public List<Customer> page(Pager<Customer> page){
    	return customerMapper.page(page);
    }
    
    public List<Team> team(Pager<Customer> page){
    	return customerMapper.team(page);
    }
    
    public List<Customer> query(Map<String,Object> conditions){
    	return customerMapper.query(conditions);
    }
    
    public int update(Customer customer){
    	return customerMapper.update(customer);
    }
    
    
}